Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

feat(vitest-environment-nuxt): allow mocking imports within setup files #274

Merged
merged 13 commits into from
Aug 14, 2023

Conversation

huang-julien
Copy link
Contributor

@huang-julien huang-julien commented Jul 22, 2023

Hi 👋

This PR allows mocking imports within setup files.

Currently if we can only use mockNuxtImport either within the setup file or within the test file.

The idea behind this PR is to use the new vi.hoisted from vitest 0.31 to set globally a __NUXT_VITEST_MOCKS variable which would contains all imports (mocked or not).

mockNuxtImport will instead of returning the module modified, assign the module within the __NUXT_VITEST_MOCKS variable, modify the mocked import and return __NUXT_VITEST_MOCKS

// if not, the module won't be mocked
if(shouldPrependMockHoist) {
importPathsList.forEach((p) => {
s.append( `\n import ${JSON.stringify(p)};\n`)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way to keep triggering vitest mock without importing the module ?

@huang-julien huang-julien marked this pull request as ready for review July 24, 2023 18:44
@danielroe danielroe changed the title feat(vitest-environnement-nuxt): allow mocking imports within setup files feat(vitest-environment-nuxt): allow mocking imports within setup files Jul 24, 2023
@huang-julien
Copy link
Contributor Author

hmmm... that's strange i can't reproduce the issue locally

@huang-julien huang-julien marked this pull request as draft July 24, 2023 20:05
@huang-julien huang-julien marked this pull request as ready for review July 24, 2023 20:20
@huang-julien huang-julien marked this pull request as draft July 24, 2023 21:04
@huang-julien
Copy link
Contributor Author

huang-julien commented Jul 24, 2023

OW NUXT_VITEST_DEV_TEST=true doesn't use the vitest.config file ? The setup file isn't ran
I swear i was like this for 15 whole minutes 😂
image

@huang-julien huang-julien marked this pull request as ready for review July 27, 2023 22:05
@danielroe danielroe merged commit 2d5a91e into danielroe:main Aug 14, 2023
6 checks passed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants